'use strict'

import * as uuid from 'uuid'
import * as path from 'path'
import * as fs from 'fs'

class FileService {
    saveFile(file) {
        try {

            if (!fs.existsSync('static')){
                fs.mkdirSync('static')
                console.log('static created')
            }   if (!fs.existsSync('static/pictures')){
                    fs.mkdirSync('static/pictures')
                    console.log('pictures created')
            }

            const fileName = `${uuid.v4()}.jpg`
            const filePath = path.resolve('static/pictures', fileName)
            file.mv(filePath)
            return fileName
        } catch (error) {
            console.log(error);
        }
    }
}

export default new FileService()